package cn.tl.base.controller;

import cn.tl.org.utils.AjaxResult;
import cn.tl.org.utils.FastDfsUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastdfs")
public class FastDfsController {

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        System.out.println(file.getSize());
        System.out.println(file.getOriginalFilename());
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀
        String exName = originalFilename.substring(originalFilename.indexOf(".") + 1);

        //1.处理图片 -保存到磁盘上面，返回图片存储位置，
        //2.前端发送请求的时候 logo就不为空
        try {
            String path = FastDfsUtil.upload(file.getBytes(), exName);
            return AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }



    }
}
